﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyHealthOnline.Business;
using MyHealthOnline.Dal.Generated.CollectionClasses;

namespace MyHealthOnline.Specialist
{
    public partial class CurrentWeek : System.Web.UI.Page
    {
        public ConsultCollection Consults
        {
            get
            {
                return ViewState["Consults"] as ConsultCollection;
            }
            set
            {
                ViewState["Consults"] = value;
            }
        }

        public int weekNo
        {
            get
            {
                return Convert.ToInt32(ViewState["weekNo"]);
            }
            set
            {
                ViewState["weekNo"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                weekNo = Utility.GetWeek(DateTime.Now);
                SetPatientsConsultsDataSource(ConsultList);
            }
        }

        protected void btnPrev_Click(object sender, EventArgs e)
        {
            weekNo = weekNo - 1;
            SetLastWeekGrid();
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            weekNo = weekNo + 1;
            SetLastWeekGrid();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            int nr = 0;
            var patientList = ConsultBusiness.FilterConsults( ConsultList,txtSearchPatient.Text);

            SetPatientsConsultsDataSource(patientList);
        }

        private void SetPatientsConsultsDataSource(ConsultCollection consultList)
        {
            Consults = consultList;

            SetCurrentWeekGrid();
            SetLastWeekGrid();
        }

        private void SetCurrentWeekGrid()
        {
            var thisWeekConsultList = ConsultBusiness.GetCurrentConsults( Consults);

            string emptyDataText = "No data for current week";
            ucCurrentConsultations.SetGrid(thisWeekConsultList, emptyDataText);
        }
        private void SetLastWeekGrid()
        {
            DateTime startOfWeek = Utility.GetStartOfWeek(weekNo-1);
            ConsultCollection lastWeekConsultList = ConsultBusiness.GetConsults(Consults, startOfWeek);

            string emptyDataText="No data for past week";
            ucPastConsultations.SetGrid(lastWeekConsultList,emptyDataText );
        
            lblWeekNo.Text = weekNo.ToString();
            lblFrom.Text = startOfWeek.ToString();
            lblTo.Text = startOfWeek.AddDays(7).ToString();
        }

        
        private ConsultCollection ConsultList
        {
            get
            {
                return ConsultBusiness.GetConsults();
            }
        }

    }
}